package com.zenitera.bigdata.state;

import com.zenitera.bigdata.bean.WaterSensor;
import com.zenitera.bigdata.util.BigdataUtil;
import org.apache.flink.api.common.state.MapState;
import org.apache.flink.api.common.state.MapStateDescriptor;
import org.apache.flink.api.common.typeinfo.TypeHint;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

/**
 * MapState
 */
public class Flink02_State_Key_MapState {
    public static void main(String[] args) throws Exception {

        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);


        env
                .socketTextStream("localhost", 6666)
                .map(line -> {
                    String[] data = line.split(",");
                    return new WaterSensor(
                            data[0],
                            Long.valueOf(data[1]),
                            Integer.valueOf(data[2])
                    );
                })
                .keyBy(WaterSensor::getId)
                .process(new KeyedProcessFunction<String, WaterSensor, String>() {


                    private MapState<Integer, Object> vcMapState;

                    @Override
                    public void open(Configuration parameters) throws Exception {
                        vcMapState = getRuntimeContext().getMapState(
                                new MapStateDescriptor<Integer, Object>(
                                        "vcMapState",
                                        TypeInformation.of(new TypeHint<Integer>() {}),
                                        TypeInformation.of(new TypeHint<Object>() {})
                                )

                        );
                    }

                    @Override
                    public void processElement(WaterSensor value,
                                               Context ctx,
                                               Collector<String> out) throws Exception {

                        vcMapState.put(value.getVc(), new Object());


                        Iterable<Integer> vcs = vcMapState.keys();

                        out.collect(ctx.getCurrentKey() + " VC: " + BigdataUtil.toList(vcs));
                    }
                })
                .print();

        env.execute();
    }
}
/*
D:\netcat-win32-1.12>nc64.exe -lp 6666
s1,1,2
s1,1,4
s1,1,6
s1,1,8
s1,1,10
s1,1,12
s1,1,16
s1,1,18
s1,1,20
s1,1,22
--------------------------
s1 VC: [2]
s1 VC: [2, 4]
s1 VC: [2, 4, 6]
s1 VC: [2, 4, 6, 8]
s1 VC: [2, 4, 6, 8, 10]
s1 VC: [2, 4, 6, 8, 10, 12]
s1 VC: [16, 2, 4, 6, 8, 10, 12]
s1 VC: [16, 2, 18, 4, 6, 8, 10, 12]
s1 VC: [16, 2, 18, 4, 20, 6, 8, 10, 12]
s1 VC: [16, 2, 18, 4, 20, 6, 22, 8, 10, 12]
 */